var express = require('express');
var router = express.Router();

const db = require('./db/db');


const { sendData } = require('./common/common')


/* GET users listing. */
router.get('/', function (req, res, next) {
  res.send('home模块');
});

//获取首页banner图列表接口
router.get('/getBannerList', function (req, res, next) {
  let { id} = req.query;
  if(id){
     let sql = `select * from poster where type='banner'`;
     db.query(sql,(err,data)=>{
            if(data&&data.length){
                sendData(res,0,'获取banner图片列表成功',data);
            }else{
                sendData(res,1,'获取失败，找不到数据');
            }
     })
  }else{
    sendData(res,1,'获取失败，缺少用户ID');
  }
});

//获取首页--nav导航栏列表接口
router.get('/getNavList', function (req, res, next) {
    let { id} = req.query;
    if(id){
       let sql = `select * from poster where type='nav'`;
       db.query(sql,(err,data)=>{
              if(data&&data.length){
                  sendData(res,0,'获取导航栏列表成功',data);
              }else{
                  sendData(res,1,'获取失败，找不到数据');
              }
       })
    }else{
      sendData(res,1,'获取失败，缺少用户ID');
    }
  });
  
  //首页-微淘众筹-好物榜-限时购-热销榜
router.get('/getHomeGoods', function (req, res, next) {
    let { id} = req.query;
    if(id){
        let resData = {
            joinGoods:[],
            hotGoods:[],
            perfectGoods:[],
            timeGoods:[]
        }
        let sql =`select * from join_goods limit 3`;
        db.query(sql,(err,data)=>{
            if(data&&data.length){
                resData.joinGoods = data;
                let sql = `select * from hot_goods limit 3`;
                db.query(sql,(err,data)=>{
                    if(data&& data.length){
                        resData.hotGoods = data;
                        let sql = `select * from perfect_goods limit 3`;
                        db.query(sql,(err,data)=>{
                            if(data&& data.length){
                                resData.perfectGoods = data;
                                let sql = `select * from time_goods`;
                                db.query(sql,(err,data)=>{
                                    if(data&&data.length){
                                        resData.timeGoods = data
                                        sendData(res,0,'获取数据成功',resData)
                                    }else{
                                        sendData(res,1,'获取失败，找不到数据');
                                    }
                                })
                            }else{
                                sendData(res,1,'获取失败，找不到数据');
                            }
                        })
                    }else{
                        sendData(res,1,'获取失败，找不到数据');
                    }
                })
            }else{
                sendData(res,1,'获取失败，找不到数据');
            }
        })
        








    }else{
      sendData(res,1,'获取失败，缺少用户ID');
    }
  });


  //首页-推荐列表数据获取
  router.get('/getRecommendList', function (req, res, next) {
    let { id} = req.query;
    if(id){
       let sql = `select * from recommend_goods `;
       db.query(sql,(err,data)=>{
              if(data&&data.length){
                  sendData(res,0,'获取推荐列表数据成功',data);
              }else{
                  sendData(res,1,'获取失败，找不到数据');
              }
       })
    }else{
      sendData(res,1,'获取失败，缺少用户ID');
    }
  });








module.exports = router;
